package sharetime.user.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.security.GrantedAuthority;

import sharetime.core.EntityObject;

@Entity
@Table(name = "role")
public class Role extends EntityObject implements GrantedAuthority {

	private static final long serialVersionUID = -4545230334425117495L;

	public static final String DEFAULT_ROLE_NAME = "user";

	private String name;

	private String description;

	protected Set<User> users = new HashSet<User>();

	private Set<PermissionBase> permissions;

	@OneToMany(mappedBy = "role")
	public Set<PermissionBase> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<PermissionBase> permissions) {
		this.permissions = permissions;
	}

	@Column(name = "description")
	public String getDescription() {
		return description;
	}

	@Column(name = "name", nullable = false)
	public String getName() {
		return name;
	}

	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER, mappedBy = "roles")
	public Set<User> getUsers() {
		return users;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append(
				this.name.toString()).toString();
	}

	@Override
	@Transient
	public String getAuthority() {
		return this.getName();
	}

	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		return 0;
	}
}
